package classes;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

public class part2 extends JFrame implements ActionListener{
	
	public static final int WINDOW_WIDTH = 400;
	public static final int WINDOW_HEIGHT = 400;
	public static final int CIRCLE_DIAMETER = 100;
	public static final int X_CIRCLE = 50;
	public static final int Y_CIRCLE = 50;
	public static final int NUMBER_OF_CHAR = 30;
	public static final int MAX_NUM_DART = 10000000;
	int numberOfDarts=0;
	final int pixels[][] = new int[MAX_NUM_DART][3];
	boolean flag = false;
	
	private JTextField number;
	private JLabel resultLabel;
	
	
	private class DrawingPanel extends JPanel
	 {
		public void paintComponent(Graphics g)
	 		{
			super.paintComponent(g);
			setBackground(Color.YELLOW);
			
			g.drawOval(X_CIRCLE, Y_CIRCLE, CIRCLE_DIAMETER, CIRCLE_DIAMETER);
			g.drawRect(X_CIRCLE, X_CIRCLE, CIRCLE_DIAMETER, CIRCLE_DIAMETER);
	 		if(flag){
	 			 for (int idx = 0; idx < numberOfDarts; idx++){
	 				 if (pixels[idx][2] == 1){
	 				 g.setColor(Color.red);}
	 				 else {
	 					g.setColor(Color.blue);}
	 				 
	 				 g.drawLine(pixels[idx][0],pixels[idx][1],pixels[idx][0],pixels[idx][1]);
	 			 }
	 		}
	 		}
	
	 }
	
	
	
	public static void main(String[] args)
	 {
		part2 window = new part2();
		window.setVisible(true);
	 }
	public part2()
	{
	 super("MonteCarlo");
	 setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 setLayout(new GridLayout(2, 1));
	 getContentPane().setBackground(Color.white);
	 
	 DrawingPanel circlePanel = new DrawingPanel();
	 JPanel dartPanel = new JPanel();
	 
	 
	 circlePanel.setLayout(new BorderLayout());
	 dartPanel.setLayout(new FlowLayout());
	 dartPanel.setBackground(Color.GREEN);
	 
	 number = new JTextField(NUMBER_OF_CHAR);
	
	 JLabel dartLabel = new JLabel("Enter number of darts:");
	 resultLabel = new JLabel();
	 //dartPanel.add(dartLabel, BorderLayout.LINE_END);
	
	 JButton actionButton = new JButton("Calculate Pi");
	 actionButton.addActionListener(this);
	 dartPanel.add(dartLabel);
	 dartPanel.add(number, BorderLayout.NORTH);
	 dartPanel.add(actionButton);
	 dartPanel.add(resultLabel,BorderLayout.SOUTH);
	 add(circlePanel);
	 add(dartPanel);
	 
	
	}

	
	
	public void actionPerformed(ActionEvent e)
	{
	 /*String actionCommand = e.getActionCommand();
	 if (actionCommand.equals("Click me"))
		 number.setText("Hello " + name.getText());
	 else if (actionCommand.equals("Clear"))
		 number.setText("");
	 else
		 number.setText("Unexpected error.");*/
		
		if (Integer.parseInt(number.getText()) != 0){
			if (Integer.parseInt(number.getText())<=MAX_NUM_DART){
			numberOfDarts = Integer.parseInt(number.getText());
			}
			else{
				resultLabel.setForeground(Color.red);
				resultLabel.setText("Please enter a number between 1-" + MAX_NUM_DART);
				return;
			}
		}
		
		
		float Pi;
    	int x = 0;
    	int y = 0;
    	int M = 0;
    	
    	
		 Random randomGenerator = new Random();
       
		 double startTime = System.currentTimeMillis();
		 
		 for (int idx = 0; idx < numberOfDarts; idx++){
            
        	 int aStart = X_CIRCLE;
        	 int aEnd = X_CIRCLE + CIRCLE_DIAMETER;
        	 if ( aStart > aEnd ) {
        	      throw new IllegalArgumentException("Start cannot exceed End.");
        	 }
        	    //get the range, casting to long to avoid overflow problems
        	long range = (long)aEnd - (long)aStart + 1;
        	    // compute a fraction of the range, 0 <= frac < range
        	long fraction = (long)(range * randomGenerator.nextDouble());
        	x =  (int)(fraction + aStart); 
        	fraction = (long)(range * randomGenerator.nextDouble());
        	y =  (int)(fraction + aStart); 
        	 
            pixels[idx][0] = x;
            pixels[idx][1] = y;
          
            if ((((x - 100)*(x - 100)) + ((y-100)*(y-100))) < (CIRCLE_DIAMETER/2)*(CIRCLE_DIAMETER/2)) {
        		M++;
        		pixels[idx][2] = 1;
        	}
            else{
            	  pixels[idx][2] = 0;
            }
            
         }

		 double duration = System.currentTimeMillis() - startTime ;

		 
         flag = true;
         Pi =  (float) 4*M/numberOfDarts;
         resultLabel.setForeground(Color.black);
         resultLabel.setText("The Pi is: " +  Pi + " & process time is:"
        		 + duration + "ms");
		this.paintComponents(getGraphics());
	}

}


